bit=$(shell getconf LONG_BIT)
sub_dirs=public db action main
ver=1.0.0
mj_pub_dir=../../mangjingpublic
mjc_dir=../../mangjingc/src/public

ifdef ver
ifeq ($(bit), 32)
	rel_dir = mangjings-$(ver).i386
else
	rel_dir = mangjings-$(ver).x86_64
endif
endif

all:
	@for subdir in $(sub_dirs);\
	do\
		cd $$subdir;make;cd -;\
	done
	@echo '===== code total lines '`find -name \mjs_*.[c,h]* | xargs cat | wc -l` '====='

init:uninit
	@mkdir -p ./depend/duye/lib
	@cp -rf $(mj_pub_dir)/depend/duye/include ./depend/duye/
	@cp -rf $(mj_pub_dir)/depend/duye/lib/$(bit)/* ./depend/duye/lib/
	@cd ./depend/duye/lib; ln -snf libduyebase.so.* libduyebase.so; ln -snf libduyenets.so.* libduyenets.so
	@echo "sync duye libs [OK]"

	@rm -rf ./public/mj_net_protocol.*
	@cp -rf $(mjc_dir)/mj_net_protocol.* ./public
	@echo "sync mj_net_protocol.* [OK]"

	@echo "sync mangjing_msg.xml file [OK]"
	@cp -f $(mj_pub_dir)/conf/mangjing_msg.xml ./conf/

uninit:
	@rm -rf ./depend/duye

clean:
	@for subdir in $(sub_dirs);\
	do\
		cd $$subdir;make clean;cd -;\
	done

cleanall:unrel uninit
	@for subdir in $(sub_dirs);\
	do\
		cd $$subdir;make cleanall;cd -;\
	done
	rm -rf $(MJS_RELEASE_DIR)

rel:unrel
ifndef ver
	@#echo 'setting version please, ex : make rel ver=1.0.0'
else
	@echo "mangjings release start"
	@echo release directory = $(rel_dir)
	@rm -rf $(rel_dir)
	@mkdir -p $(rel_dir)
	@mkdir -p $(rel_dir)/bin
	@mkdir -p $(rel_dir)/lib
	@# copy bin 
	cp -f $(MJS_RELEASE_DIR)/bin/* $(rel_dir)/bin/
	@# copy mangjings libs
	cp -f $(MJS_RELEASE_DIR)/lib/*.so $(rel_dir)/lib/
	@# copy duye libs
	cp -f $(MJS_ROOT_DIR)/depend/duye/lib/libduye*.so.* $(rel_dir)/lib/
	@# strip 
	#strip $(rel_dir)/bin/* >/dev/null 2>&1
	#strip $(rel_dir)/lib/* >/dev/null 2>&1
	#strip $(rel_dir)/module/* >/dev/null 2>&1
	@# copy script
	cp -rf $(MJS_ROOT_DIR)/script $(rel_dir)
	cp -f $(rel_dir)/script/install.sh $(rel_dir)
	cp -f $(rel_dir)/script/uninstall.sh $(rel_dir)
	rm -f $(rel_dir)/script/install.sh
	rm -f $(rel_dir)/script/uninstall.sh
	sed -i 's/version=/version='"$(ver)"'/g' $(rel_dir)/install.sh
	sed -i 's/version=/version='"$(ver)"'/g' $(rel_dir)/uninstall.sh
	@# copy conf
	cp -rf $(MJS_ROOT_DIR)/conf $(rel_dir)
	@# tar
	tar czvf $(rel_dir).tar.gz $(rel_dir) >/dev/null 2>&1
	@echo "mangjings release complete"
endif

unrel:
	@rm -rf $(MJS_ROOT_DIR)/mangjings-*
